package lium.lbs.lbsim.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.widget.ImageView
import com.buz.hjcdriver.bean.AResultBean
import com.buz.hjcdriver.bean.CountryListResultBean
import com.lmlibrary.Constants
import com.lmlibrary.bean.ResponseBean
import lium.lbs.lbsim.utils.callbck.DialogCallback
import lium.lbs.lbsim.utils.ToastUtils
import com.lzy.okgo.model.Response
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.activity_regist.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.fragments.ShowCountryFragment
import lium.lbs.lbsim.utils.DesUtils
import lium.lbs.lbsim.utils.SpUtils
import java.util.HashMap
import java.util.concurrent.TimeUnit

class RegistActivity: BaseActivity() {
    override fun getLayoutId(): Int { return R.layout.activity_regist  }
    var countryBean: CountryListResultBean?=null
    var isforget:Boolean=false
    override fun initView(savedInstanceState: Bundle?) {

        if(Constants.Debug){
            val phone = SpUtils.getInstance().get(Constants.phone, "") as String
            login_phone.setText(phone+"")
        }
        intent?.let {
            if (intent.hasExtra("isforget")){
                isforget = intent.getBooleanExtra("isforget",false)
            }

        }
        if (isforget){
            agreenment_layout.visibility = View.GONE
        }
        addOnClickListeners(R.id.left_img,R.id.login_getsms,R.id.btn_start_login,R.id.showCountybtn,R.id.serveragreenment,R.id.lowagreenment)
    }
    override fun onClick(v: View?) {
        super.onClick(v)
        when(v!!.id){
            R.id.left_img ->{
                val intent = Intent(this, LoginActivity::class.java)
                startActivity(intent)
                finish()
            }
            R.id.login_getsms ->{
                getSMS()
            }
            R.id.btn_start_login -> {
                checkSMSCode()
            }
            R.id.showCountybtn -> {
                showCounty()
            }
            R.id.serveragreenment -> {
                val intent = Intent(this, WebViewActivity::class.java)
                intent.putExtra("title","服务条款")
                intent.putExtra("url",Constants.serverareenment)
                startActivity(intent)
            }
            R.id.lowagreenment -> {
                val intent = Intent(this, WebViewActivity::class.java)
                intent.putExtra("title","法律条款")
                intent.putExtra("url",Constants.lawareenment)
                startActivity(intent)
            }
        }
    }

    private fun checkSMSCode() {

        if(TextUtils.isEmpty(login_phone.text.toString()) ||
            login_phone.text.toString().length!=11){
            ToastUtils.showToast("手机号码无效")
            return
        }
        if(TextUtils.isEmpty(login_sms.text.toString()) ||
            login_sms.text.toString().length!=6){
            ToastUtils.showToast("验证码无效")
            return
        }
        val param = HashMap<String, String>()
        param["phone"] = login_phone.text.toString()
        param["code"] = login_sms.text.toString()
        param["countryCode"] = "86"
        countryBean?.let {
            param["countryCode"] = countryBean!!.code
        }
        postData("/member/memberCheckCode",param, object : DialogCallback<ResponseBean<AResultBean>>(this) {
            override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                ToastUtils.showToast(response.body().msg+"")
                val intent = Intent(this@RegistActivity, RegistSetPWDActivity::class.java)
                intent.putExtra("phone",login_phone.text.toString()+"")
                intent.putExtra("isforget",isforget)
                intent.putExtra("countryCode","86")
                countryBean?.let {
                    intent.putExtra("countryCode",countryBean!!.code+"")
                }
                intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
                startActivity(intent)
                finish()
            }
        })

    }

    private fun showCounty() {
        var showCountryFragment = ShowCountryFragment()
        showCountryFragment
        showCountryFragment.show(supportFragmentManager,"showCountryFragment")
    }

    private fun getSMS() {
        if(TextUtils.isEmpty(login_phone.text.toString()) ||
            login_phone.text.toString().length!=11){
            ToastUtils.showToast("手机号码无效")
            return
        }

        val param = HashMap<String, String>()
        val encryptedString = DesUtils.encryptDESwithBase64(login_phone.text.toString(), "Chat2019")
        param["phone"] = encryptedString

        if(isforget){
            param["type"] = "forget"
        }else{
            param["type"] = "register"
        }

        param["countryCode"] = "86"
        countryBean?.let {
            param["countryCode"] = countryBean!!.code
        }

        postData("/member/getIdentifyingCode",param, object : DialogCallback<ResponseBean<AResultBean>>(this) {
            override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                ToastUtils.showToast(response.body().msg+"")
                time(60)
            }
        })

    }

    public fun setConutryBean(countryBean:CountryListResultBean){
        this.countryBean = countryBean
        showCountybtn.text ="+" +countryBean.code
    }

    fun time(time: Long) {
        login_getsms.isEnabled=false
        login_phone.isEnabled=false
        addDisposable(Observable.interval(1, TimeUnit.SECONDS)
            .map { aLong -> time - aLong }
            .take(time + 1)
            .observeOn(AndroidSchedulers.mainThread())
            .doOnComplete {
                login_getsms.isEnabled=true
                login_getsms.text ="获取"
                login_phone.isEnabled=true
            }
            .subscribe {
                login_getsms.text = ""+it.toString()+  "秒";
            })
    }

    override fun getNetWorkData() {
    }
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN) {
            val intent = Intent(this@RegistActivity, LoginActivity::class.java)
            startActivity(intent)
            finish()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}

